<template>
	<view class="website">
		<view class="tabs">
			<view class="tabs-item" :class="{active: form.type === 1}" @tap="changeTab(1)">微官网</view>
			<view class="tabs-item" :class="{active: form.type === 2}" @tap="changeTab(2)">URL 链接</view>
		</view>
		<view class="section">
			<view v-if="form.type === 1">
				<!-- <template v-if="form.ximg">
					<view class="img">
						<image :src="form.ximg" mode="aspectFit"></image>
						<icon type="clear" size="20" @tap="remove('ximg')"/>
					</view>
				</template>
				<template v-else>
					<view class="add-section" @tap="chooseImg('ximg')">
						<view>
							<text>点击上传宣传图片</text>
							<view class="btn">编辑</view>
						</view>					
					</view>	
				</template> -->
				<div class="title">宣传图片</div>
				<view class="img-box">
					<view v-for="(item, index) in form.ximg" key="index" class="img">
						<image :src="item" mode=""></image>
						<icon type="clear" size="20" @tap="remove2(index, 'ximg')"/>
					</view>			
					<view v-if="form.ximg.length < 5" class="add-btn" @tap="chooseImg('ximg')">上传图片</view>
				</view>
				<!-- <template v-if="form.qimg">
					<view class="img">
						<image :src="form.qimg" mode="aspectFit"></image>
						<icon type="clear" size="20" @tap="remove('qimg')"/>
					</view>
				</template>
				<template v-else>
					<view class="add-section" @tap="chooseImg('qimg')">
						<view>
							<text>点击上传企业图片</text>
							<view class="btn">编辑</view>
						</view>
					</view>
				</template>	 -->
				<div class="title">企业图片</div>
				<view class="img-box">
					<view v-for="(item, index) in form.qimg" key="index" class="img">
						<image :src="item" mode=""></image>
						<icon type="clear" size="20" @tap="remove2(index, 'qimg')"/>
					</view>			
					<view v-if="form.qimg.length < 5" class="add-btn" @tap="chooseImg('qimg')">上传图片</view>
				</view>
				<view class="textarea-box">
					<!-- <view>
						<text>点击编辑企业介绍</text>
						<view class="btn">编辑</view>
					</view> -->
					<textarea v-model="form.companyIntroduction" cols="30" rows="10" placeholder="请输入企业介绍内容"></textarea>
				</view>
			</view>
			<view v-else>
				<view class="input-box">
					<input v-model="form.websiteUrl" type="text" placeholder="请输入网站地址">
				</view>
			</view>
		</view>
		<view class="btn-box">
			<view class="btn" @tap="submit">保存</view>
		</view>
	</view>
</template>

<script>
	import http from '@/utils/http.js'
	export default {
		data() {
			return {
			  value: '',			  
			  form: {
				  type: 1,
				  qimg: [],
				  ximg: [],
				  websiteUrl: '',
				  companyIntroduction: ''
			  }
			}
		},
		onLoad() {
			this.getData()
		},
		methods: {
			remove2(index, key) {
				this.form[key].splice(index, 1)
			},
			confirm(e) {
				
			},
			changeTab(n) {
				this.form.type = n
			},
			goEdit() {
				uni.navigateTo({
					url: '/pages/module/introduce'
				})
			},
			remove(key) {
				this.form[key] = ''
			},
			upload(filePath, key) {
				const fileName = filePath.split('/').pop()
				uni.uploadFile({
				      url: `https://ywtmp.hnwytkj.com/app-api/infra/file/upload?path=${fileName}`, //仅为示例，非真实的接口地址
				      filePath,
				      name: 'file',
				      // formData: {},
				      success: (uploadFileRes) => {
						const res = JSON.parse(uploadFileRes.data)
				        console.log(uploadFileRes.data);
						if (res.code === 0) {
							this.form[key].push(res.data)
						}
				      },
					  fail: (res) => {
						  console.log(res)
					  }
				    });
			},
			chooseImg(key) {
				uni.chooseImage({
				    count: 1, //默认9
				    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				    sourceType: ['album'], //从相册选择
					success: (res) => {
						console.log(res.tempFilePaths)
						this.upload(res.tempFilePaths[0], key)
					}
				});				
			},
			submit() {
				http({
					url: 'card/micro-website/create',
					method: 'put',
					data: {
						...this.form,		
						ximg: JSON.stringify(this.form.ximg),
						qimg: JSON.stringify(this.form.qimg),
						id: uni.getStorageSync('userId')
					}
				}).then(res => {				
					if(res.code === 0) {
						uni.showToast({
							title: '保存成功',
							icon: "success"
						});
					}
				})
			},
			getData() {
				const userId = uni.getStorageSync('userId')
				http({
					url: `card/micro-website/get?id=${userId}`
				}).then(res => {				
					if(res.code === 0) {
						this.form = {
							...res.data,
							qimg: res.data.qimg ? JSON.parse(res.data.qimg) : [],
							ximg: res.data.ximg ? JSON.parse(res.data.ximg) : []
						}
					}
				})		
			}
		},
	}
</script>
	
<style lang="scss" scoped>
	.website {
		min-height: 100vh;
		background-color: #F0F0F4;
		background-image: linear-gradient(180deg, #4B73FF, #F0F0F4);
		background-size: 100% 447rpx;
		background-repeat: no-repeat;
		padding: 30rpx;
		box-sizing: border-box;
		font-family: PingFang SC;
		.tabs {
			font-weight: bold;
			font-size: 34rpx;
			color: #FFFFFF;
			display: flex;		
			padding: 0 24rpx;
			.tabs-item {
				position: relative;
				padding-bottom: 36rpx;
				margin-left: 102rpx;
				&:first-child {
					margin-left: 0;
				}
				&.active {
					&::after {
						content: '';
						width: 40rpx;
						height: 6rpx;
						background: #FFFFFF;
						border-radius: 3rpx;
						position: absolute;
						bottom: 0;
						left: 50%;
						transform: translateX(-50%);
					}
				}
			}
		}
		.section {
			background: #FFFFFF;
			border-radius: 20rpx;
			margin-top: 20rpx;
			min-height: calc(100vh - 320rpx - var(--safe-area-inset-bottom));
			padding: 30rpx 30rpx calc(30rpx + var(--safe-area-inset-bottom));
			box-sizing: border-box;
			// .add-section {
			// 	height: 261rpx;
			// 	background: #E6ECFF;
			// 	border-radius: 20rpx;
			// 	border: 1rpx dashed #4B73FF;
			// 	box-sizing: border-box;
			// 	margin-bottom: 20rpx;
			// 	display: flex;
			// 	align-items: center;
			// 	justify-content: center;
			// 	text-align: center;
			// 	text {
			// 		font-weight: 500;
			// 		font-size: 28rpx;
			// 		color: #4B73FF;
			// 	}
			// 	.btn {
			// 		width: 130rpx;
			// 		height: 70rpx;
			// 		line-height: 70rpx;
			// 		text-align: center;
			// 		background: #4B73FF;
			// 		border-radius: 30rpx;
			// 		font-weight: 500;
			// 		font-size: 26rpx;
			// 		color: #FFFFFF;
			// 		margin: 0 auto;
			// 		margin-top: 20rpx;
			// 	}				
			// }
			// .img {
			// 	height: 261rpx;
			// 	border-radius: 20rpx;
			// 	margin-bottom: 20rpx;
			// 	position: relative;
			// 	image {
			// 		width: 100%;
			// 		height: 261rpx;
			// 		border-radius: 20rpx;					
			// 	}
			// 	icon {
			// 		position: absolute;
			// 		top: -20rpx;
			// 		right: -20rpx;
			// 	}
			// }
			.title {
				font-size: 28rpx;
				margin-bottom: 20rpx;
			}
			.img-box {
				display: flex;
				align-items: center;
				flex-wrap: wrap;
				.img {
					width: 162rpx;
					height: 162rpx;
					border-radius: 20rpx;
					margin-right: 20rpx;
					margin-bottom: 40rpx;
					position: relative;
					image {
						width: 162rpx;
						height: 162rpx;
						border-radius: 20rpx;					
					}
					icon {
						position: absolute;
						top: -20rpx;
						right: -20rpx;
					}
				}			
				.add-btn {
					width: 162rpx;
					height: 162rpx;
					background: #E6ECFF;
					border-radius: 20rpx;
					border: 1rpx dashed #4B73FF;
					display: flex;
					align-items: center;
					justify-content: center;
					font-weight: 500;
					font-size: 28rpx;
					color: #4B73FF;
					margin-bottom: 40rpx;
				}
			}
			.textarea-box {
				textarea {
					font-weight: 500;
					font-size: 28rpx;
					// color: #CCCCCC;
					color: #333333;
					border-radius: 10rpx;
					border: 1rpx solid #EEEEEE;
					width: 100%;
					// height: 773rpx;
					height: 261rpx;
					box-sizing: border-box;
					padding: 28rpx 24rpx;
					&::placeholder {
						color: #CCCCCC;
					}
				}
			}
			.input-box {
				input {
					border-radius: 10rpx;
					border: 1rpx solid #EEEEEE;
					font-weight: 500;
					font-size: 28rpx;
					height: 100rpx;
					padding: 0 24rpx;
				}
			}
		}
		.btn-box {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			padding: 20rpx 20rpx calc(20rpx + var(--safe-area-inset-bottom));
			box-sizing: border-box;
			background-color: #FFFFFF;
			.btn {
				height: 90rpx;
				line-height: 90rpx;
				text-align: center;
				background: #4B73FF;
				border-radius: 20rpx;
				font-weight: bold;
				font-size: 28rpx;
				color: #FFFFFF;
				// margin-top: 42rpx;
			}
		}
	}
</style>